package spec.chenli;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

//@WebServlet(name = "NewsListServlet")
public class NewsListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        try {
//            request.setCharacterEncoding("utf-8");
//            response.setCharacterEncoding("utf-8");
//            Class.forName("com.mysql.jdbc.Driver");
//            Connection con = null;
//            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/1613580007_1?characterEncoding=utf8","root", "123456789");
//            Statement s = con.createStatement();
//            ResultSet rs  = s.executeQuery("select title,publish_time，publisher FROM t_news");
//            if(!rs.next()) {
//                response.setContentType("text/html");
//                PrintWriter out = response.getWriter();
//                String h = "";
//                h += "<!DOCTYPE html>";
//                h += "<html lang=\"en\">";
//                h += "<head>";
//                h+="<meta charset=\"UTF-8\">";
//                h+="<title>新闻列表</title>";
//                //           h+="<style type='text/css'>  .td{\n" +
////                   "        border:3px solid #bbb;\n" +
////                    "        border-collapse:collapse;\n" +
////                    "        width:500px;\n" +
////                    "        margin:30px auto;\n" +
////                    "        text-align:center;\n" +
////                   "        background-color:write;\n" +
////                    "    }";
//
//                h += "</head>";
//                h += "<body>";
//                h += "<table border=\"1\" align=\"center\" width=\"300px\">";
//                if (!rs.next()) {
//                    h += "<tr> <th colspan=\"1\">新闻标题</th><td>" + rs.getString("title") + "</td></tr>";
//                    h += "<tr> <th colspan=\"1\">发布时间</th><td>" + rs.getString("publish_time") + "</td></tr>";
//                    h += "<tr> <th colspan=\"1\">发布人</th><td>" + rs.getString("publisher") + "</td></tr>";
//                }
//                h += "</table>";
//                h+="<center><ahref='"+request.getContextPath()+"/NewsListServlet'>[返回列表]</a></center>";
//                h += "</body>";
//                h += "</html>";
//                out.write(html);
//            }
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符型
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
           try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = null;
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/1613580007_1?characterEncoding=utf8", "root", "123456789");
            Statement s = con.createStatement();
            ResultSet rs = null;
           // ResultSetMetaData m=null;
            rs = s.executeQuery("select * from t_news ");
           // m=rs.getMetaData();
           // int coulumns=m.getColumnCount();
                    String html = "";
                    html += "<head>";
                    html += "<title>新闻列表</title>";
                    html+="<script type=\"text/javascript\">\n" +
                            "function delStu(x){\n" +
                            "var btnId=x.id;\n" +
                            "var id=btnId.substring(4);\n" +
                            "var u=\"http://localhost:8080/deletenew?id=\"+id;\n" +
                            "window.open(u);\n" +
                            "}\n" +
                            "function xiugai(a){\n" +
                            "var xgId=a.id;\n" +
                            "var id=xgId.substring(4);\n" +
                            "var i=\"http://localhost:8080/xiugainews?id=\"+id;\n" +
                            "window.open(i);\n" +
                            "}\n" +
                            "function chax(b){\n" +
                            "var chaId=b.id;\n" +
                            "var id=chaId.substring(4);\n" +
                            "var j=\"http://localhost:8080/chaxun?id=\"+id;\n" +
                            "window.open(j)\n" +
                            "}\n";
                    html+="</script>";
                    html += "<html>";
                    html += "</head>";
                    html += "<body>";
                    html += "<table border='1' align='center' width=’1000px' hegin='1200px' cellpadding='1' cellspacing='1'>";
                    html+="<caption><h1>新闻列表</h1></caption>";
                    html+="<tr><th>序号:</th><th>新闻标题:</th><th>发布时间:</th><th>发布人:</th><th>删除:</th><th>修改:</th><th>查询:</th></tr>";

                    while (rs.next()) {
                    html+="<tr><td>"+rs.getString("id")+"</td><td>"+rs.getString("title")+"</td><td>"+rs.getString("publish_time")+"</td><td>"+rs.getString("publisher")
                            +"<td><input type=button value='删除' id='btn+"+rs.getString("id")+"' onclick='delStu(this)'</td><td><input type=button value='修改' id='xg+"+rs.getString("id")+"' onclick='xiugai(this)' ></td>" +
                            "<td><input type=button value='查询' id='cha+"+rs.getString("id")+"' onclick='chax(this)' ></td><tr>";
                    }
               html += "<table>";
               html+="<p></p>";
               html+="<p></P>";
               html+="<p></P>";
               html+="<p></P>";
               html+="<center><input type='button' style='width: 80px;height:50px'  value='增加' onClick='location.href=\"http://localhost:8080/NewsAdd.html\"'</center>";
               html += "</body>";
                    html += "</html>";
                    out.println(html);
                    out.flush();
                    out.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}